#include "datacontainer.h"

DataContainer::DataContainer(QObject *parent) :
    QObject(parent)
{

}

void DataContainer::addContigDataPacket( ContigDataPacket* cdp )
{
    contigDataPackets << cdp;
    hashedContigs.insert(cdp->getContigId(), contigDataPackets.size()-1);
}

void DataContainer::addGenomeElementDataPacket( GenomeElementDataPacket* gdps)
{
    genomeElementDataPackets << gdps;
    hashedGenomeElements.insert(gdps->getSeqId(), genomeElementDataPackets.size()-1);
}

void DataContainer::appendContigs(QList<  QPointer<ContigDataPacket> > cdps)
{
    for(int i =0; i < cdps.size(); i++)
    {
        addContigDataPacket(cdps[i]);
    }
}

void DataContainer::appendGenomeElements(QList< QPointer<GenomeElementDataPacket> > gdps)
{
    for(int i =0; i < gdps.size(); i++)
    {
        addGenomeElementDataPacket(gdps[i]);
    }
}

bool DataContainer::containsContigDataPacket( QString key )
{
    return hashedContigs.contains(key);
}

bool DataContainer::containsGenomeElementDataPacket( QString key)
{
    return hashedGenomeElements.contains(key);
}

void DataContainer::removeContigDataPacket( QString key )
{
    int idx = hashedContigs.take(key);
    if( idx == -1 )
        return;
    contigDataPackets.removeAt(idx);
    for(int i = idx; i < contigDataPackets.size(); i++)
    {
        hashedContigs.take(key);
        hashedContigs.insert(key, i);
    }
}

void DataContainer::removeGenomeElementDataPacket( QString key )
{
    int idx = hashedGenomeElements.take(key);
    if( idx == -1 )
        return;
    genomeElementDataPackets.removeAt(idx);
    for(int i = idx; i < genomeElementDataPackets.size(); i++)
    {
        hashedGenomeElements.take(key);
        hashedGenomeElements.insert(key, i);
    }
}

ContigDataPacket* DataContainer::getContigDataPacket(int idx)
{
    if( idx < 0 || idx >= contigDataPackets.size() )
        return NULL;
    return contigDataPackets[idx];
}

ContigDataPacket* DataContainer::getContigDataPacket(QString key)
{
    int idx = hashedContigs.value(key);
    if( idx < 0 || idx >= contigDataPackets.size() )
        return NULL;
    return contigDataPackets[idx];
}

GenomeElementDataPacket* DataContainer::getGenomeElementDataPacket(int idx)
{
    if( idx < 0 || idx >= genomeElementDataPackets.size() )
        return NULL;
    return genomeElementDataPackets[idx];
}

GenomeElementDataPacket* DataContainer::getGenomeElementDataPacket(QString key)
{
    int idx = hashedGenomeElements.value(key);
    if( idx < 0 || idx >= genomeElementDataPackets.size() )
        return NULL;
    return genomeElementDataPackets[idx];
}

int DataContainer::getNumGenomeElements()
{
    return genomeElementDataPackets.size();
}

int DataContainer::getNumContigs()
{
    return contigDataPackets.size();
}

void DataContainer::clear()
{
    hashedGenomeElements.clear();
    genomeElementDataPackets.clear();
    hashedContigs.clear();
    contigDataPackets.clear();
}

void DataContainer::clearAndDelete()
{
    //qDeleteAll(genomeElementDataPackets);
    //qDeleteAll(contigDataPackets);

    qDebug() << "clearing out data container " << endl;

    while( !contigDataPackets.isEmpty() )
    {
        ContigDataPacket* c = contigDataPackets.takeFirst();
        if( c != 0 )
        {
            delete c;
            c = 0;
        }
    }

//    while( ! genomeElementDataPackets.isEmpty() )
//    {
//        GenomeElementDataPacket* g = genomeElementDataPackets.takeFirst();
//        qDebug() << "value of genome element ptr: " << g << endl;
//        if( g != 0 )
//        {
//            delete g;
//            g = 0;
//        }
//    }

    //this->clear();
}
